package com.fastjrun.util;

import com.aliyun.alidns20150109.models.*;
import com.aliyun.darabonba.env.EnvClient;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.fastjrun.Sample;
import com.aliyun.alidns20150109.Client;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Stan
 */
@Service
public class AliYunUtil {


    private Client client = null;

    public AliYunUtil() {
        Config config = new Config();
        // 您的AccessKey ID
        config.accessKeyId = "LTAIUy8yKiS8EZYJ";
        // 您的AccessKey Secret
        config.accessKeySecret = "XQFfTfAR1eAX2YsIcF9c10mWwuCDAS";
        // 您的可用区ID
        config.regionId = "cn-hangzhou";
        try {
            this.client = new Client(config);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Initialization  初始化公共请求参数
     */
    @Bean
    public Client getClient(String regionId) throws Exception {
        Config config = new Config();
        // 您的AccessKey ID
        config.accessKeyId = EnvClient.getEnv("ACCESS_KEY_ID");
        // 您的AccessKey Secret
        config.accessKeySecret = EnvClient.getEnv("ACCESS_KEY_SECRET");
        // 您的可用区ID
        config.regionId = regionId;
        return new Client(config);
    }

    /**
     * 获取主域名的所有解析记录列表
     */
    public DescribeDomainRecordsResponse describeDomainRecords(String domainName, String rr, String recordType) throws Exception {
        DescribeDomainRecordsRequest req = new DescribeDomainRecordsRequest();
        // 主域名
        req.domainName = domainName;
        // 主机记录
        req.RRKeyWord = rr;
        // 解析记录类型
        req.type = recordType;
        try {
            DescribeDomainRecordsResponse resp = client.describeDomainRecords(req);
            com.aliyun.teaconsole.Client.log("-------------------获取主域名的所有解析记录列表--------------------");
            com.aliyun.teaconsole.Client.log(Common.toJSONString(TeaModel.buildMap(resp)));
            return resp;
        } catch (TeaException error) {
            com.aliyun.teaconsole.Client.log(error.message);
        } catch (Exception exception) {
            TeaException error = new TeaException(exception.getMessage(), exception);
            com.aliyun.teaconsole.Client.log(error.message);
        }
        return null;
    }

    /**
     * 修改解析记录
     */
    public void updateDomainRecord(UpdateDomainRecordRequest req) throws Exception {
        try {
            UpdateDomainRecordResponse resp = client.updateDomainRecord(req);
            com.aliyun.teaconsole.Client.log("-------------------修改解析记录--------------------");
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
        } catch (TeaException error) {
            com.aliyun.teaconsole.Client.log(error.message);
        } catch (Exception exception) {
            TeaException error = new TeaException(exception.getMessage(), exception);
            com.aliyun.teaconsole.Client.log(error.message);
        }
    }

    public static void main(String[] argStrs) throws Exception {
        List<String> args = java.util.Arrays.asList(argStrs);
        String regionid = args.get(0);
        String currentHostIp = args.get(1);
        String domainName = args.get(2);
        String rr = args.get(3);
        String recordType = args.get(4);
        com.aliyun.alidns20150109.Client client = Sample.Initialization(regionid);
        DescribeDomainRecordsResponse response = Sample.DescribeDomainRecords(client, domainName, rr, recordType);
        assert response != null;
        if (Common.isUnset(TeaModel.buildMap(response.body.domainRecords.record.get(0)))) {
            com.aliyun.teaconsole.Client.log("错误参数！");
            return;
        } else if (Common.isUnset(TeaModel.buildMap(response))) {
            com.aliyun.teaconsole.Client.log("错误参数！");
            return;
        }

        DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecordsRecord record = response.body.domainRecords.record.get(0);
        // 记录ID
        String recordId = record.recordId;
        // 记录值
        String recordsValue = record.value;
        com.aliyun.teaconsole.Client.log("-------------------当前主机公网IP为：" + currentHostIp + "--------------------");
        if (!com.aliyun.teautil.Common.equalString(currentHostIp, recordsValue)) {
            // 修改解析记录
            UpdateDomainRecordRequest req = new UpdateDomainRecordRequest();
            // 主机记录
            req.RR = rr;
            // 记录ID
            req.recordId = recordId;
            // 将主机记录值改为当前主机IP
            req.value = currentHostIp;
            // 解析记录类型
            req.type = recordType;
            Sample.UpdateDomainRecord(client, req);
        }

    }
}
